package QRCG;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class NewGameServe
 */
@WebServlet("/NewGameServe")
public class NewGameServe extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NewGameServe() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);
		
		String nickname = (String)session.getAttribute("nickname");

		String gamename= request.getParameter("gamename");
		String description= request.getParameter("description");
		String location= request.getParameter("Loc");
		System.out.println(location);
		String begin_date= request.getParameter("begin_date");
		String end_date= request.getParameter("end_date");
		String level= request.getParameter("level_count");
		String activation= request.getParameter("activation");
		int act=0;
		
		if(activation.compareTo("Active")==0)
		{
			act=1;
		}
		
		String query_loc = "SELECT location_id FROM Locations WHERE location_name='"+location+"'";

		String query_creatorid = "SELECT user_id FROM Users WHERE nickname='"+nickname+"'";
		
		deneme dk = new deneme();
		
		try {
			dk.connect();
			String loc_id = dk.selectOneThing(query_loc);
			String cre_id = dk.selectOneThing(query_creatorid);
			String query = "INSERT INTO Games (creator_id,name,description,begin_date,end_date,location_id,is_active,level_count)" +
					"VALUES("+cre_id+",'"+gamename+"','"+description+"','"+begin_date+"','"+end_date+"',"+loc_id+",'"+act+"','"+level+"')";
			
			if(dk.execute(query))
			{
				session.setAttribute("nickname", nickname);
				response.sendRedirect("GameCreated.jsp");				
			}

				
			    
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

		
		
	}

}
